package com.itheima.leetcode.od.c.backtracking;

import java.util.Arrays;
import java.util.Scanner;

/**
 * <h3>连续出牌数量</h3>
 */
public class NumberOfConsecutiveCardsPlayed {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] nums = Arrays.stream(sc.nextLine().split(" "))
                .mapToInt(Integer::parseInt)
                .toArray();
        String[] colors = sc.nextLine().split(" ");

        System.out.println(getResult(nums, colors));
    }

    public static int getResult(int[] nums, String[] colors) {
        int n = nums.length;
        Card[] cards = new Card[n];
        for (int i = 0; i < n; i++) {
            cards[i] = new Card(nums[i], colors[i]);
        }

        int[] ans = {0};
        boolean[] visited = new boolean[n];
        dfs(cards, visited, null, 0, ans);
        return ans[0];
    }

    public static void dfs(Card[] cards, boolean[] visited, Card pre, int count, int[] ans) {
        ans[0] = Math.max(ans[0], count);

        for (int i = 0; i < cards.length; i++) {
            if (visited[i]) {
                continue;
            }

            Card cur = cards[i];
            if (pre != null && pre.num != cur.num && pre.color != cur.color) {
                continue;
            }

            visited[i] = true;
            dfs(cards, visited, cur, count + 1, ans);
            visited[i] = false;
        }
    }

    static class Card {
        int num;
        char color;

        public Card(int num, String color) {
            this.num = num;
            this.color = color.charAt(0);
        }
    }
}